﻿Imports DataObject
Imports BusinessObject
Imports Microsoft.ApplicationBlocks.Data

Public Class frmHangHoaVatTuDetail

    Private ptyHangHoa As New HangHoa
    Private oHangHoa As New BusHangHoa
    Private oFunction As New hDCommonFunction

    Private Sub frmHangHoaVatTuDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Me.eActionMode = EditMode.AddNew) Then
            Me.Text = "Thêm mới hàng hóa, vật tư"
        Else
            Me.Text = "Sửa hàng hóa, vật tư"
        End If
        txtMaHH.Focus()
        LoadAllCombo()
        DataBindingToControl(GroupBox1)
    End Sub

    Private Sub LoadAllCombo()
        Dim ds As New DataSet
        ds.Clear()
        If (Not ds.Tables("LoaiHangHoa") Is Nothing) Then
            ds.Tables("LoaiHangHoa").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_LoaiHangHoa_By_HoatDong", ds, New String() {"LoaiHangHoa"})
        With cboLoaiHH
            .DataSource = ds.Tables("LoaiHangHoa")
            .DisplayMember = "TenLoaiHH"
            .ValueMember = "SoLoaiHH"
            .DisplayLayout.Load(PathLayout & "cboLoaiHangHoa.lyt")
        End With

        If (Not ds.Tables("DoiTuong") Is Nothing) Then
            ds.Tables("DoiTuong").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_DoiTuongs", ds, New String() {"DoiTuong"})
        With cboNhaCungCap
            .DataSource = ds.Tables("DoiTuong")
            .DisplayMember = "TenDoiTuong"
            .ValueMember = "SoDoiTuong"
            .DisplayLayout.Load(PathLayout & "cboDoiTuong.lyt")
        End With
    End Sub

    Public Overrides Function ValidateData() As Boolean
        If txtMaHH.Text = "" Then
            MessageBox.Show("Mã hàng hóa, vật tư không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtMaHH.Focus()
            Return False
        End If
        If txtTenHH.Text = "" Then
            MessageBox.Show("Tên hàng hóa, vật tư được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtTenHH.Focus()
            Return False
        End If
        Return True
    End Function

    Protected Overrides Function SaveDictionary() As Boolean
        Dim bResult As Boolean = False
        Try
            ptyHangHoa.MaHH = txtMaHH.Text
            ptyHangHoa.TenHH = txtTenHH.Text
            ptyHangHoa.SoLoaiHH = cboLoaiHH.Value
            ptyHangHoa.DonVi = txtDonViTinh.Text
            ptyHangHoa.DonViChuyenDoi = txtDonViChuyenDoi.Text
            ptyHangHoa.TyLeChuyenDoi = ceTyLeChuyenDoi.Value
            ptyHangHoa.ThoiHanBaoHanh = txtThoiHanBaoHanh.Text
            ptyHangHoa.SoLuongToiThieu = neSoLuongToiThieu.Value
            ptyHangHoa.DonGiaMua = ceDonGiaMua.Value
            ptyHangHoa.DonGiaBan = ceDonGiaBan.Value
            ptyHangHoa.ThueSuat = cboThueSuat.Value
            ptyHangHoa.SoDoiTuong = cboNhaCungCap.Value
            ptyHangHoa.DienGiai = txtDienGiai.Text
            ptyHangHoa.HoatDong = chkHoatDong.CheckedValue

            If (eActionMode = EditMode.AddNew) Then
                oHangHoa.InsertHangHoa(sConn, ptyHangHoa)
            ElseIf (eActionMode = EditMode.Edit) Then
                ptyHangHoa.SoHH = RowDetail.Item("SoHH")
                oHangHoa.UpdateHangHoa(sConn, ptyHangHoa)
            End If
            bResult = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            bResult = False
        End Try
        Return bResult
    End Function

    Private Sub cboLoaiHH_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboLoaiHH.EditorButtonClick
        Dim oRow As DataRow
        Dim ds As New DataSet
        Dim f As New frmLoaiVatTuHangHoaDetail
        Dim ListBindingSource As New System.Windows.Forms.BindingSource
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_LoaiHangHoas", ds, New String() {"LoaiHangHoa"})
        oRow = ds.Tables("LoaiHangHoa").NewRow
        With oRow
            .Item("MaLoaiHH") = ""
            .Item("TenLoaiHH") = ""
            .Item("HoatDong") = True
        End With
        ds.Tables("LoaiHangHoa").Rows.Add(oRow)
        ListBindingSource.DataSource = ds.Tables("LoaiHangHoa")
        ListBindingSource.MoveLast()
        With f
            .ActionMode = EditMode.AddNew
            .BindingSource = ListBindingSource
            .RowDetail = oRow
            .ShowDialog()
        End With
        LoadAllCombo()
    End Sub

    Private Sub cboNhaCungCap_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboNhaCungCap.EditorButtonClick
        Dim oRow As DataRow
        Dim ds As New DataSet
        Dim f As New frmNhanVienDetail
        Dim ListBindingSource As New System.Windows.Forms.BindingSource
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_NhanViens", ds, New String() {"NhanVien"})
        oRow = ds.Tables("NhanVien").NewRow
        With oRow
            .Item("MaNV") = ""
            .Item("TenNV") = ""
            .Item("GioiTinh") = True
        End With
        ds.Tables("NhanVien").Rows.Add(oRow)
        ListBindingSource.DataSource = ds.Tables("NhanVien")
        ListBindingSource.MoveLast()
        With f
            .ActionMode = EditMode.AddNew
            .BindingSource = ListBindingSource
            .RowDetail = oRow
            .ShowDialog()
        End With
        LoadAllCombo()
    End Sub

End Class
